package com.sky.controller.admin;


import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategroryContraller {
    @Autowired
    private CategoryService categoryService;
    //分类分页查询
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询：{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    //启用和禁用分类
    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用分类")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("启用或禁用分类：{}",id);
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    //新增分类
    @PostMapping
    @ApiOperation("新增分类")
    public Result save(@RequestBody Category category){
        log.info("新增分类：{}",category);
        categoryService.save(category);
        return Result.success();
    }
    //根据id进行删除
    @DeleteMapping
    public Result deleteById(Long id){
        log.info("根据id进行删除：{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }

    //修改数据回显
    @GetMapping("/{id}")
    public Result<Category> getById(Long id){
        log.info("修改数据回显：{}",id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    @PutMapping
    public Result update(@RequestBody Category category){
        log.info("修改分类：{}",category);
        categoryService.update(category);
        return Result.success();
    }

    //查询分类
    @GetMapping("/list")
    @ApiOperation("查询分类11")
    public Result list(Integer type){
        log.info("查询分类：{}",type);
       // PageResult pageResul = categoryService.list(type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
